package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.bean.TimeLineRecord;
import com.smalls.newvideotwo.db.Record;
import com.smalls.newvideotwo.utils.ImageLoaderUtil;
import com.smalls.newvideotwo.utils.MeasureUtil;

import java.util.ArrayList;
import java.util.List;

public class NewTestListViewAdapter extends BaseAdapter {
    private Context context;
    private ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    private List<TimeLineRecord> list;
    private View.OnClickListener mOnClickListener=null;
    private View.OnFocusChangeListener mOnFocusChangeListener=null;
    private LayoutInflater mInflater;
    private NewHistoryAdapter nearByInfoImgsAdapter;
    private int type;

    static class ViewHolder {
        RecyclerView gv_albums;
        TextView info_num_tv;
        TextView info_timerange_tv;
        LinearLayout llear_layout;
        RelativeLayout recyclerViewRe;

        ViewHolder() {
        }
    }

    public NewTestListViewAdapter(Context context, List<TimeLineRecord> list,View.OnClickListener listener ,View.OnFocusChangeListener listenr, int type) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        this.mOnClickListener = listener;
        this.mOnFocusChangeListener = listenr;
        this.type = type;
    }

    public List<TimeLineRecord> getList() {
        return this.list;
    }

    public void setList(List<TimeLineRecord> list) {
        this.list = list;
    }

    public int getCount() {
        return this.list == null ? 0 : this.list.size();
    }

    public Object getItem(int arg0) {
        return this.list == null ? null : (TimeLineRecord) this.list.get(arg0);
    }

    public long getItemId(int arg0) {
        return (long) (this.list == null ? null : Integer.valueOf(arg0)).intValue();
    }

    public void clearDatas() {
        if (this.list != null && this.list.size() > 0) {
            this.list.clear();
        }
    }

    public void changeData(List<TimeLineRecord> list) {
        if (list != null) {
            this.list = list;
        } else {
            this.list = new ArrayList();
        }
        notifyDataSetChanged();
    }

    public List<TimeLineRecord> getDatas() {
        return this.list;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mInflater.inflate(R.layout.item_listview, null);
            holder = new ViewHolder();
            holder.info_timerange_tv = (TextView) convertView.findViewById(R.id.new_info_timerange_tv);
            holder.info_num_tv = (TextView) convertView.findViewById(R.id.new_info_num_tv);
            holder.llear_layout = (LinearLayout) convertView.findViewById(R.id.new_llear_layout);
            holder.gv_albums = (RecyclerView) convertView.findViewById(R.id.new_gv_albums);
            holder.recyclerViewRe = (RelativeLayout) convertView.findViewById(R.id.new_recycler_view_re);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TimeLineRecord bean = (TimeLineRecord) this.list.get(position);
        if (bean.getTimeTv() != null) {
            holder.info_timerange_tv.setText(bean.getTimeTv());
        }
        if (bean.getRecords() != null) {
            holder.info_num_tv.setText(this.context.getString(R.string.total_movelist_number) + bean.getRecords().size() + this.context.getString(R.string.total_movelist_numset));
        }
        initInfoImages(holder.gv_albums,holder.recyclerViewRe, bean.getRecords());
        return convertView;
    }

    public int initInfoImages(final RecyclerView gv_images,RelativeLayout relativeLayout, final List<Record> Albumls) {
        if (Albumls != null && Albumls.size() > 0) {
            int line = Albumls.size()%5==0?Albumls.size()/5:Albumls.size()/5+1;
            RelativeLayout.LayoutParams imageSwitcherPa = (RelativeLayout.LayoutParams) relativeLayout
                    .getLayoutParams();
            //imageSwitcherPa.width = (int) MeasureUtil.getWidthSize(182);
            imageSwitcherPa.height = (int) MeasureUtil.getHeightSize(line*436);
            //imageSwitcherPa.topMargin = (int) MeasureUtil.getHeightSize(36);
            imageSwitcherPa.leftMargin=(int) MeasureUtil.getWidthSize(224);
            imageSwitcherPa.rightMargin=(int) MeasureUtil.getWidthSize(104);
            relativeLayout.setLayoutParams(imageSwitcherPa);
            ///this.nearByInfoImgsAdapter = new NewHistoryAdapter(Albumls);
            nearByInfoImgsAdapter.setOnClickListenter(mOnClickListener);
            nearByInfoImgsAdapter.setOnFocusListener(mOnFocusChangeListener);
            gv_images.setAdapter(this.nearByInfoImgsAdapter);
            return line*436;
        }
        return 0;
    }
}
